<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增资料专题')" />
    <th:block th:include="include :: jasny-bootstrap-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-materialTopic-add">
            <div class="form-group">    
                <label class="col-sm-3 control-label">专题名称：</label>
                <div class="col-sm-8">
                    <input name="name" class="form-control" type="text">
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">首页推荐：</label>
                <div class="col-sm-8">
                    <label class="toggle-switch switch-solid"/>
                    <input type="checkbox" id="main" >
                    <span></span>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">图标：</label>
                <div class="col-sm-8">
                    <input id="cover" name="avatar" class="form-control" type="hidden">
                    <div class="fileinput fileinput-new" data-provides="fileinput">
                        <div class="fileinput-new thumbnail" style="width:100px; height: 100px;">
                            <img >
                        </div>
                        <div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 100px; max-height: 100px;"></div>
                        <div>
                            <span class="btn btn-white btn-file"><span class="fileinput-new">选择图片</span><span class="fileinput-exists">更改</span><input type="file" class="image"></span>
                            <a href="#" class="btn btn-white fileinput-exists" data-dismiss="fileinput">清除</a>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: jasny-bootstrap-js" />
    <script type="text/javascript">
        var prefix = ctx + "dict/materialTopic"
        $("#form-materialTopic-add").validate({
            focusCleanup: true
        });

        $('.image').on('change.bs.fileinput', function (e) {
        	var file = $(this)[0].files[0];
        	var data = new FormData();
	        data.append("file", file);
	        $.ajax({
	            type: "POST",
	            url: ctx + "common/uploadOSS",
	            data: data,
	            cache: false,
	            contentType: false,
	            processData: false,
	            dataType: 'json',
	            success: function(result) {
	            	$('#cover').val(result.url);
	            },
	            error: function(error) {
	                alert("图片上传失败。");
	            }
	        });

        });

        function submitHandler() {
            if ($.validate.form()) {

                var data = $('#form-materialTopic-add').serializeArray()
                var main = $("input[id='main']").is(':checked') == true ? 1 : 0;
            	data.push({"name": "main", "value": main});

                $.operate.save(prefix + "/add", data);
            }
        }

    </script>
</body>
</html>